<template>
  <div>
    <div class="modal">
      <div class="modal-bg" @click="UnitCloses"></div>
      <div class="pub-w">
        <div class="rele-title" v-show="tianjiadanyuan">添加单元<span @click="UnitCloses">X</span></div>
        <div class="rele-title" v-show="xiugaidanyuan">修改单元<span @click="UnitCloses">X</span></div>
        <div class="pub-list">
          <div class="pub-li">
            <div class="pub-title">单元名称</div>
            <div class="pub-input"><input type="text" name="" v-model="unittitle"></div>
          </div>
          <div class="pub-li">
            <div class="pub-title">排序值</div>
            <div class="pub-input"><input type="number" name="" v-model="sort"></div>
          </div>
          <div class="pub-li">
            <button v-show="tianjiadanyuan" @click="tianjiaDY">添加</button>
            <button v-show="xiugaidanyuan" @click="xiugaiDY">修改</button>
            <button @click="UnitCloses">取消</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script type="text/babel">
  export default {
    props: {
      lookdanyuan:{
        type:Function
      },
      LDmodal:{
        type:Function
      },
      UnitClose: {
        type: Function
      },
      LDid: {
        type: Number
      },
      DYid:{
        type:Number
      }
    },
    data() {
      return {
        unittitle: '',
        sort: '',
        tianjiadanyuan:true,
        xiugaidanyuan:false,
      }
    },
    methods: {
      UnitCloses(){
        this.sort = '';
        this.unittitle = '';
        this.UnitClose();
      },
      TJgoDY(){
        this.tianjiadanyuan=true;
          this.xiugaidanyuan=false;
      },
      XGgoDY(){
        this.tianjiadanyuan=false;
          this.xiugaidanyuan=true;
      },
      tianjiaDY() {
        this.$http.post('/newapi/Premisesunit/add', {
          premisesbuilding_id: this.LDid,
          title: this.unittitle,
          sort: this.sort
        }).then(res => {
          if (res.data.status === 200) {
            this.UnitClose();
            this.lookdanyuan(this.LDid);
            this.$store.state.messageString = res.data.message;
            this.LDmodal();
            this.sort = '';
            this.unittitle = '';
          }else {
            this.$store.state.messageString = res.data.message;
            this.LDmodal();
          }
        }).catch(err => {
          console.log(err)
        })
      },
      xiugaiDY(){
        this.$http.post('/newapi/Premisesunit/add', {
          id:this.$store.state.DYid,
          premisesbuilding_id: this.LDid,
          title: this.unittitle,
          sort: this.sort
        }).then(res => {
          // console.log('单元' + this.DYid + ';' + '楼栋' + this.LDid);
          if (res.data.status === 200) {
            this.UnitClose();
            this.lookdanyuan(this.LDid);
            this.$store.state.messageString = res.data.message;
            this.LDmodal();
            this.unittitle = '';
            this.sort = ''
          }else {
            this.$store.state.messageString = res.data.message;
            this.LDmodal();
          }
        }).catch(err => {
          console.log(err)
        })
      },
      gettitle() {
        let that = this;
        that.$http.get('/newapi/Premisesunit/details', {
          params: {
            id: that.$store.state.DYid
          }
        }).then(res => {
          that.unittitle = res.data.res.title;
          that.sort = res.data.res.sort;
        }).catch(err => {
          console.log(err)
        });
      }
    },
    computed: {
      danyuanIDs(){
        return this.danyuanID
      }
    }
  }
</script>
<style scoped>
  .pub-li button {
    width: 100px;
    font-size: 1.1em;
    background: #415dba;
    color: #ffffff;
    border: none;
    float: left;
    border-radius: 5px;
    line-height: 35px;
  }
  .pub-li button:last-child {
    float: right;
    background: none;
    border: 1px solid #415dba;
    color: #415dba
  }
  .pub-list {
    width: 100%;
    float: left;
    text-align: center;
    margin-top: 40px;
  }
  .pub-input {
    width: 70%;
    float: left;
  }
  .pub-input input {
    width: 100%;
    line-height: 27px;
    height: 27px;
    float: left;
    box-sizing: border-box;
    border-radius: 5px;
    border: 1px solid #d3d3d3;
  }
  .pub-li {
    margin-top: 20px;
    width: 80%;
    display: inline-block;
  }
  .pub-title {
    width: 30%;
    line-height: 27px;
    text-align: center;
    font-size: 1.1em;
    float: left;
  }
  .pub-w {
    width: 20%;
    left: 40%;
    height: 310px;
    top: 50%;
    overflow: hidden;
    margin-top: -155px;
    background: #ffffff;
    border-radius: 5px;
    position: absolute;
    z-index: 10;
  }
</style>
